from django.shortcuts import render, HttpResponse


# Create your views here.
def index(request):
    '''首页

    准备工作
    '''
    markdown = """# CSDN PYTHON 闯关训练营第三章

## 环境

```text
Python >= 3.6 32位
Django === 2.2.2
Mysql  >= 5.5
```

其他包
```text
pymysql
mysqlclient
```

## 搭建步骤

* 创建数据库

    ```mysql
    CREATE DATABASE IF NOT EXISTS work03 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
    ```

* 创建 Django 项目

    ```bash
    python -m django-admin startproject work03
    cd work03
    ```

* 配置数据库

    ```python
    # work03/settings.py

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',   # 数据库引擎
            'NAME': 'work03',                       # 你要存储数据的库名，事先要创建之
            'USER': 'root',                         # 数据库用户名
            'PASSWORD': '123',                      # 密码
            'HOST': 'localhost',                    # 主机
            'PORT': '3306',                         # 数据库使用的端口
        }
    }
    ```

* 配置模板目录

    ```python
    # work03/settings.py

    TEMPLATES = [
        {
            ...
            'DIRS': [os.path.join(BASE_DIR, 'templates'), ],
            ...
    ]
    ```

* 配置静态资源目录

    ```python
    # work03/settings.py

    STATIC_URL = '/static/'
    STATICFILES_DIRS = (os.path.join(BASE_DIR, 'statics'), )
    ```

* 配置语言和时区

    ```python
    # work03/settings.py

    ```

* 初始化数据库

    ```bash
    python manage.py migrate
    ```

* 创建管理员用户

    ```bash
    python manage.py createsuperuser
    ```

* 创建 APP

    ```bash
    REM 创建
    python manage.py startspp home
    ```

    * APP --> 视图 [home/views.py]

        ```python
        from django.shortcuts import HttpResponse
        def index(request):
            return HttpResponse('Hello Django!')
        ```

    * APP --> 路由 [home/urls.py]

        ```python
        from django.urls import path, re_path
        from . import views

        urlpatterns = [
            path('', views.index, name='index'),    # 首页
        ]
        ```

    * APP --> 路由映射 [work03/urls.py]

        ```python
        from django.urls import include
        urlpatterns = [
            ...
            path('', include('home.urls')),
        ]
        ```

"""
    context = {
        'title': 'Home',
        'markdown': markdown,
    }
    return render(request, 'marked.html', context)
